package su.order.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import su.base.Constants;
import su.base.dao.SuBaseDAO;
import su.order.dao.IOrderDAO;
import su.order.dto.OrderDto;
import su.order.entity.OrderEntity;


@Repository
public class OrderDAO extends SuBaseDAO implements IOrderDAO {
	
	
	public List<Map<String,Object>> queryByPage(OrderDto dto) throws SQLException {
		String sql = "select o.*,uc.customername,uy.ywyname from su_order o join su_user_customer uc on o.customer_id=uc.id join su_user_ywy uy on o.ywy_id=uy.id where o.status=1";
		if(StringUtils.isNotBlank(dto.getS1())){//id
			if(dto.getS1().indexOf(",")>-1){
				sql += " and id o.in ("+dto.getS1()+")";
			}else{
				sql += " and o.id="+dto.getS1();
			}
		}
		sql += " ORDER BY o.id desc";
		
		return super.queryByPage(sql, new RowMapper<Map<String,Object>>() {

			public Map<String,Object> mapRow(ResultSet rs, int arg1) throws SQLException {
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("orderId", rs.getInt("id"));
				map.put("orderType", rs.getInt("order_type"));
				map.put("customerId", rs.getInt("customer_id"));
				map.put("ywyId", rs.getInt("ywy_id"));
				map.put("operator", rs.getString("operator"));
				map.put("contact", rs.getString("contact"));
				map.put("invoice", rs.getInt("invoice"));
				map.put("invoiceStatus", rs.getInt("invoice_status"));
				map.put("tax", rs.getDouble("tax"));
				map.put("odesc", rs.getString("odesc"));
				map.put("orderCreateTime", rs.getDate("order_create_time"));
				map.put("storageTime", rs.getDate("storage_time"));
				map.put("shipmentTime", rs.getDate("shipment_time"));
				map.put("status", rs.getInt("status"));
				map.put("settlementStatus", rs.getInt("settlement_status"));
				map.put("totalShipment", rs.getDouble("total_shipment"));
				map.put("totalPrice", rs.getDouble("totalprice"));
				map.put("totalPayment", rs.getDouble("totalpayment"));
				map.put("totalPay", rs.getDouble("totalpay"));
				map.put("customerName", rs.getString("customername"));//客户
				map.put("ywyName", rs.getString("ywyname"));//业务员
				return map;
			}
			
		}, dto);
	}
	

	
	
	public OrderEntity getById(int id) throws SQLException {
		String sql = "select * from su_order where status=1 and id=? limit 1";
		return super.queryForObject(sql, new Object[]{id}, new RowMapper<OrderEntity>() {
			public OrderEntity mapRow(ResultSet rs, int value) throws SQLException {
				OrderEntity entity = new OrderEntity();
				entity.setId(rs.getInt("id"));
				entity.setOrderType(rs.getInt("order_type"));
				entity.setCustomerId(rs.getInt("customer_id"));
				entity.setYwyId(rs.getInt("ywy_id"));
				entity.setOperator(rs.getString("operator"));
				entity.setContact(rs.getString("contact"));
				entity.setInvoice(rs.getInt("invoice"));
				entity.setInvoiceStatus(rs.getInt("invoice_status"));
				entity.setTax(rs.getDouble("tax"));
				entity.setOdesc(rs.getString("odesc"));
				entity.setOrderCreateTime(rs.getDate("order_create_time"));
				entity.setStorageTime(rs.getDate("storage_time"));
				entity.setShipmentTime(rs.getDate("shipment_time"));
				entity.setStatus(rs.getInt("status"));
				entity.setSettlementStatus(rs.getInt("settlement_status"));
				entity.setTotalShipment(rs.getDouble("total_shipment"));
				entity.setTotalprice(rs.getDouble("totalprice"));
				entity.setTotalpayment(rs.getDouble("totalpayment"));
				entity.setTotalpay(rs.getDouble("totalpay"));
				return entity;
			}
		});
	}

	
	public void remove(int id) throws SQLException {
		final String sql = "update su_order set status=? where id=?";
		super.update(sql, new Object[]{Constants.STATUS_DISABLE,id});
	}
	
	
	
	public int insert(OrderEntity o) throws SQLException {
		String sql = "insert into su_order(order_type ,customer_id ,ywy_id ,operator ,contact ,invoice ,invoice_status ,tax ,odesc ,order_create_time ,storage_time ,shipment_time ,status ,settlement_status ,totalprice ,totalpayment ,totalpay ) values(? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? )";
		KeyHolder kh = super.updateBackKey(sql, new Object[]{o.getOrderType() ,o.getCustomerId() ,o.getYwyId() ,o.getOperator() ,o.getContact() ,o.getInvoice() ,o.getInvoiceStatus() ,o.getTax() ,o.getOdesc() ,o.getOrderCreateTime() ,o.getStorageTime() ,o.getShipmentTime() ,o.getStatus() ,o.getSettlementStatus() ,o.getTotalprice() ,o.getTotalpayment() ,o.getTotalpay() });
		if(kh == null)return 0;
		int orderId = kh.getKey().intValue();
		return orderId;
	}
	
	public void update(OrderEntity o) throws SQLException {
		StringBuffer updateSql = new StringBuffer("update su_order set ");
		List<Object> args = new ArrayList<Object>();
		args.add(o.getYwyId());
		updateSql.append("ywy_id=?").append(",");
		args.add(o.getOperator());
		updateSql.append("operator=?").append(",");
		args.add(o.getContact());
		updateSql.append("contact=?").append(",");
		args.add(o.getInvoice());
		updateSql.append("invoice=?").append(",");
		args.add(o.getOdesc());
		updateSql.append("odesc=?").append(",");
		
		args.add(o.getId());
			
		
		String sql = updateSql.substring(0, updateSql.length()-1) + " where id = ? "; 
		super.update(sql, args.toArray());
	}
	
	public void updateTotal(OrderEntity o) throws SQLException{
		String sql = "update su_order set totalprice=?,totalpayment=? where id=?";
		Object[] args = new Object[]{o.getTotalprice(),o.getTotalpayment(),o.getId()};
		super.update(sql, args);
	}

}

